package cn.com.cis.service;

import java.security.SecureRandom;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.cis.domain.Role;
import cn.com.cis.domain.User;
import cn.com.cis.persistence.UserMapper;
import cn.com.cis.plugins.mybatis.PageHelper;
import cn.com.cis.utils.security.PasswordHash;

@Service
@Transactional
public class UserService {

	// hash校验值生成算法和散列次数
	public static final int SALT_BYTE_SIZE = 24;

    @Autowired
    private UserMapper mapper;

    @Transactional(readOnly = true)
    public User selectUserById(int id) {
        return mapper.selectUserById(id);
    }

    @Transactional(readOnly = true)
    public User selectUserByUsernameAndPassword(String username, String password) {
        return mapper.selectUserByUsernameAndPassword(username, password);
    }
    
    @Transactional(readOnly = true)
    public User selectUserByUsername(String username) {
        return mapper.selectUserByUsername(username);
    }

    public void insertUser(User user) {
    	setUserPassword(user);
        mapper.insertUser(user);
    }
    
    private void setUserPassword(User user){
    	// 盐值使用基于加密的伪随机数生成器（Cryptographically Secure Pseudo-Random Number Generator – CSPRNG）来生成
    	SecureRandom random = new SecureRandom();
		byte[] hashSalt = new byte[SALT_BYTE_SIZE];
		random.nextBytes(hashSalt);
    	
		String salt = PasswordHash.toHex(hashSalt);
    	user.setSalt(salt);
    	
    	String passwordHash = PasswordHash.createHash(user.getPassword().toCharArray(), hashSalt);
    	user.setPassword(passwordHash);
    }
    
    public void deleteUser(int id) {
        mapper.deleteUser(id);
    }

    public void updateUser(User user) {
        mapper.updateUser(user);
    }

    public void updateUserAndPassword(User user) {
    	setUserPassword(user);
		mapper.updateUserAndPassword(user);
	}

    @Transactional(readOnly = true)
    public List<User> selectAllUser(int page) {
        PageHelper.startPage(page, 10);
        return mapper.selectAllUser();
    }
    
    @Transactional(readOnly = true)
    public List<User> selectAllUser() {
        return mapper.selectAllUser();
    }
    
    @Transactional(readOnly = true)
    public List<Role> selectUserRoles(int userId) {
        return mapper.selectUserRoles(userId);
    }
    
    @Transactional(readOnly = true)
    public List<String> selectPermissionsForUser(int userId){
    	return mapper.selectPermissionsForUser(userId);
    }

	public void addPermissionForUser(Integer userId, String code) {
		mapper.addPermissionForUser(userId, code);
	}

	public void deletePermissionFromUser(Integer userId, String code) {
		mapper.deletePermissionFromUser(userId, code);
	}

	public void clearRoles(int id) {
		mapper.clearRoles(id);
	}

	public void addRole(int id, String roleId) {
		mapper.addRole(id, roleId);
	}

	public void clearPermissions(int id) {
		mapper.clearPermissions(id);
	}
}
